package com.mcxx.modules.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.mcxx.modules.system.service.IdcardValidateService;
import com.mcxx.util.CommonUtil;
import com.mcxx.util.HttpServiceUtil;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;


@Service
@TaTransactional
public class IdcardValidateServiceImpl implements IdcardValidateService {

    @Value("${yinhai.police.url:http://10.22.197.101:10000/engine/rest/}")
    private String serviceUrl;
    @Value("${yinhai.police.serviceKey:getSfzInfoAndImg}")
    private String serviceKey;
    @Value("${yinhai.data-sync.access-key:hnsocialrelief}")
    private String accessKey;

    private final static Logger logger = LoggerFactory.getLogger(IdcardValidateServiceImpl.class);


    @Override
    public String getIdcardInfo(String idcard,String name) {
        Map<String,String> paramMap = new HashMap<>();
        
        paramMap.put("zjhm",idcard);
        paramMap.put("xm",name);
        String url = serviceUrl+serviceKey;
        String res;
        try {
            res = HttpServiceUtil.callServiceWithNativeHttp(accessKey,url,paramMap,"1.0");
        } catch (Exception e) {
            logger.info("查询公安身份证接口异常");
            logger.error(e.getMessage(),e);
            return null;
        }
        logger.info("查询公安身份证接口响应res:{}",res);
        JSONObject resJson = null;
        try {
            resJson = JSONObject.parseObject(res);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            logger.info("查询公安身份证接口响应转换json异常");
            return null;
        }
        
        if(resJson!=null && resJson.containsKey("code")&&"1".equals(resJson.getString("code"))){
            
            if(resJson.containsKey("biz_data") && !CommonUtil.invalidArgs(resJson.getJSONObject("biz_data"))  && resJson.getJSONObject("biz_data").containsKey("msg")){
                String msg = resJson.getJSONObject("biz_data").getString("msg");
                return msg;
            }
        }
        return null;
    }
}
